import threading

class SharedCounter:
    '''
    A counter object that can be shared by multiple threads.
    '''
    _lock = threading.RLock()
    def __init__(self, initial_value = 0):
        self._value = initial_value

    def incr(self,delta=1):
        '''
        Increment the counter with locking
        '''
        with SharedCounter._lock:
            print("incr", self._value)
            self._value += delta

    def decr(self,delta=1):
        '''
        Decrement the counter with locking
        '''
        with SharedCounter._lock:
            print("decr", self._value)
            self.incr(-delta)



sh1 = SharedCounter()

th1 = threading.Thread(target=sh1.decr)
th1.start()

th2 = threading.Thread(target=sh1.incr)
th2.start()